package com.open.capacity.reimburse.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.entity.BasePayItem;
import com.open.capacity.vo.TreeVo;
import io.swagger.annotations.Api;
import org.apache.commons.collections.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.BaseBgtEco;
import com.open.capacity.reimburse.service.BaseBgtEcoService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 经济分类科目
 *
 * @author
 * @email
 * @date 2023-11-01 09:14:48
 */
@RestController
@RequestMapping("basebgtecos")
@Api(tags = "经济分类科目")
public class BaseBgtEcoController {

    @Autowired
    private BaseBgtEcoService baseBgtEcoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('base_bgt_eco:basebgteco:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = baseBgtEcoService.pageAll(params);
        return pageResult;
    }


    /**
     * 查询公司tree
     *
     * @param parentId
     * @return
     */
    @PreAuthorize("hasAnyAuthority('base_bgt_eco:basebgteco:list')")
    @GetMapping("/tree")
    public Result<List<TreeVo>> treeBgtecos(@RequestParam(value = "parentId", defaultValue = "0") String parentId) {
        Map<String, Object> params = new HashMap<>();
        params.put("parentId", parentId);
        params.put("status", 1);
        List<BaseBgtEco> list = baseBgtEcoService.findAll(params);
        List<TreeVo> treeList = new TreeList();
        list.forEach(m -> {
            TreeVo node = new TreeVo();
            node.setId(m.getId());
            node.setText(m.getName());
            node.setTitle(m.getName());
            node.setValue(m.getId());
            node.setShowcheck(false);
            node.setCheckstate(0);
            if (m.getParentId().equals("0")) {
                node.setIsexpand(true);
            } else {
                node.setIsexpand(false);
            }
            node.setComplete(false);
            node.setParentId(m.getParentId());
            treeList.add(node);
        });

        return Result.succeed(TreeVo.RecursiveBuilder(treeList, parentId));
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('base_bgt_eco:basebgteco:list')")
    public Result findById(String id) {
        return Result.succeed(baseBgtEcoService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('base_bgt_eco:basebgteco:save')")
    public Result save(@RequestBody @Valid BaseBgtEco baseBgtEco, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            baseBgtEcoService.save(baseBgtEco);
        } else {
            baseBgtEco.setId(keyValue);
            baseBgtEcoService.update(baseBgtEco);
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('base_bgt_eco:basebgteco:delete')")
    public Result delete(@PathVariable String id) {
        baseBgtEcoService.delete(id);
        return Result.succeed("删除成功");
    }
}
